package com.progroup.kyrsmoney.preferences;

import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.progroup.kyrsmoney.R;

public class PreferencesActivity extends Activity {
	
	public static String INTERVAL_USER_TIME = "interval_user_time";
	public static String SAVING_BATTERY = "saving_battery";
	public static String VISIBLE_IMAGE= "visible_image";
	public static String VISIBLE_COORDINAT= "visible_coordinat";
	public static String LINE_WEIGTH= "line_weigth";
	public static String STATISTIC= "statistic";
	public static String LANG= "lang";
	public static String RUS= "ru";
	public static String UA= "ua";
	int HOURS, MINETS;
	String [] time ;
	String [] weigth = {"1","2","3","4","5","6","7"};
	String[] NAME_BANK;
	String[] BUY;
	String[] SALE;
	Spinner spinner, spinner_line, spinner_lang;
	CheckBox checkbox, checkBoxImage, checkBoxCoordinats;
	Intent intent;
	ProgressDialog dialog;
	SharedPreferences pref;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		pref = PreferenceManager.getDefaultSharedPreferences(this);
		
		
		
		time = new String[]{getText(R.string.pref_time1).toString(), getText(R.string.pref_time2).toString(), getText(R.string.pref_time3).toString() };
		
		setContentView(R.layout.pref_layout);
		setTitle(R.string.menu_preference);
		
		
		
		
		spinner = (Spinner)findViewById(R.id.pref_spinner_timer);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, time);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setSelection(Integer.parseInt(pref.getAll().get(INTERVAL_USER_TIME)+""));
		spinner.setOnItemSelectedListener(spinnSelectListener);
		checkbox = (CheckBox)findViewById(R.id.pref_checkBox_battery);
		
		if(pref.getBoolean(SAVING_BATTERY, false)){
			
			checkbox.setChecked(true);
			checkbox.setText(R.string.check_box_save_battery);
		}else{
			
			checkbox.setChecked(false);
			checkbox.setText(R.string.check_box_not_save_battery);
		}
		checkbox.setOnCheckedChangeListener(changeListener);
		
		
		checkBoxImage = (CheckBox)findViewById(R.id.pref_checkbox_image);
		
		if(pref.getBoolean(VISIBLE_IMAGE, true)){
			checkBoxImage.setChecked(true);
		}else{
			checkBoxImage.setChecked(false);
		}
		
		checkBoxImage.setOnCheckedChangeListener(changeListener);
		
		EditText edit = (EditText)findViewById(R.id.pref_editText1);
		edit.setHint(setTextDay(pref.getInt(STATISTIC, 10)));
		edit.setOnEditorActionListener(new OnEditorActionListener() {
			
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				// TODO Auto-generated method stub
				//Checks.CToast(PreferencesActivity.this, v.getText().toString());
				int i = Integer.parseInt(v.getText().toString());
				if(i<90&i>10){
					//Checks.CToast(PreferencesActivity.this, v.getText().toString());
					pref.edit().putInt(STATISTIC, i).commit();
					Log.i("EDITTEXT1", i+"");
					v.setText("");
					v.setHint(setTextDay(i));
				}else if (i<=10){
					Log.i("EDITTEXT2", 10+"");
					pref.edit().putInt(STATISTIC, 10).commit();
					v.setText("");
					v.setHint(10+" "+getText(R.string.pref_day1).toString());
				}else{
					Log.i("EDITTEXT2", 90+"");
					pref.edit().putInt(STATISTIC, 90).commit();
					v.setText("");
					v.setHint(90+" "+getText(R.string.pref_day1).toString());
					//Checks.CToast(PreferencesActivity.this, v.getText().toString());
				}
				return false;
			}
		});
		
		
		spinner_line= (Spinner)findViewById(R.id.pref_spinner_graphic_weigthline);
		ArrayAdapter<String> adapter_weigth = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, weigth);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner_line.setAdapter(adapter_weigth);
		
		spinner_line.setSelection(Integer.parseInt(pref.getAll().get(LINE_WEIGTH)+"")-1);
		spinner_line.setOnItemSelectedListener(spinn_lineSelectListener);
		
		
		checkBoxCoordinats = (CheckBox)findViewById(R.id.pref_checkbox_graphic_coordinats);
		checkBoxCoordinats.setChecked(pref.getBoolean(VISIBLE_COORDINAT, false));
		checkBoxCoordinats.setOnCheckedChangeListener(changeListener);
		
	}
	
	
	public OnItemSelectedListener spinnSelectListener = new OnItemSelectedListener() {
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			
			SharedPreferences pref1 = PreferenceManager.getDefaultSharedPreferences(PreferencesActivity.this);
			pref1.edit().putString(INTERVAL_USER_TIME, arg2+"").commit();
			Log.i("SPINNER", arg2+"");
			//Toast.makeText(PreferencesActivity.this, "Период обновления курса установлен.", Toast.LENGTH_LONG).show();
		}
		
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
		}
	};
	
	public OnItemSelectedListener spinn_lineSelectListener = new OnItemSelectedListener() {
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			SharedPreferences pref2 = PreferenceManager.getDefaultSharedPreferences(PreferencesActivity.this);
			pref2.edit().putInt(LINE_WEIGTH, arg2+1).commit();
			//Toast.makeText(PreferencesActivity.this, arg2, Toast.LENGTH_LONG).show();
			//Toast.makeText(PreferencesActivity.this, "Период обновления курса установлен.", Toast.LENGTH_LONG).show();
		}
		
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
	};
	
	
	
	
	public OnCheckedChangeListener changeListener = new OnCheckedChangeListener() {
		
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			// TODO Auto-generated method stub
			SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(PreferencesActivity.this);
			switch (buttonView.getId()) {
			case R.id.pref_checkBox_battery:
				
				pref.edit().putBoolean(SAVING_BATTERY, isChecked).commit();
				if(isChecked){
					checkbox.setText(R.string.check_box_save_battery);
					Toast.makeText(PreferencesActivity.this, getText(R.string.check_box_save_battery1), Toast.LENGTH_LONG).show();
				}else{
					Toast.makeText(PreferencesActivity.this, getText(R.string.check_box_save_battery2), Toast.LENGTH_LONG).show();
					checkbox.setText(R.string.check_box_not_save_battery);
				}
				break;
				
			case R.id.pref_checkbox_image:
				
				
				pref.edit().putBoolean(VISIBLE_IMAGE, isChecked).commit();
				
				break;
			case R.id.pref_checkbox_graphic_coordinats:
				
				
				pref.edit().putBoolean(VISIBLE_COORDINAT, isChecked).commit();
				
				break;
				
			default:
				break;
			}
			
		}
		
	};
	
	
	
	
	
	public static void addPref(Context context){
		SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(context);
		SharedPreferences.Editor  editor= pref.edit();
		//Log.i("dsjkd", pref.contains(APPOPEN)+"");
		//if(!pref.contains(STATISTIC)){
		
		if(!pref.contains(STATISTIC)){
			Log.i("SETPREF!!!!", "+++STATISTIC");
			editor.putInt(STATISTIC, 10);
			editor.commit();
		}
		if(!pref.contains(SAVING_BATTERY)){
			Log.i("SETPREF!!!!", "+++SAVING_BATTERY");
			editor.putBoolean(SAVING_BATTERY, false);
			editor.commit();
		}
		if(!pref.contains(INTERVAL_USER_TIME)){
			Log.i("SETPREF!!!!", "+++INTERVAL_USER_TIME");
			editor.putString(INTERVAL_USER_TIME, "0");
			editor.commit();
		}
		if(!pref.contains(VISIBLE_IMAGE)){
			Log.i("SETPREF!!!!", "+++VISIBLE_IMAGE");
			editor.putBoolean(VISIBLE_IMAGE, true);
			editor.commit();
		}
		if(!pref.contains(VISIBLE_COORDINAT)){
			Log.i("SETPREF!!!!", "+++VISIBLE_COORDINAT");
			editor.putBoolean(VISIBLE_COORDINAT, false);
			editor.commit();
		}
		if(!pref.contains(LINE_WEIGTH)){
			Log.i("SETPREF!!!!", "+++LINE_WEIGTH");
			editor.putInt(LINE_WEIGTH, 1);
			editor.commit();
		}
	}
	
	
	private String setTextDay(int i){
		String s = i+"";
		String r =s.substring(1);
		int e = Integer.parseInt(r);
		if(i==11){
			return i+" "+getText(R.string.pref_day1).toString();
		}else if(e==1)
		{
			return s+" "+getText(R.string.pref_day2).toString();
		}
		else if(e==2|e==3|e==4)
		{
			return s+" "+getText(R.string.pref_day3).toString();
		}
		else {
			return s+" "+getText(R.string.pref_day1).toString();
		}
	}
	
	
	public static void setpreference(Context context){
		SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(context);
		SharedPreferences.Editor  editor= pref.edit();
		editor.putBoolean("checkbox1", true);
		editor.putBoolean("checkbox2", false);
		editor.putBoolean("checkbox3", false);
		editor.putBoolean(SAVING_BATTERY, false);
		editor.putString(INTERVAL_USER_TIME, "0");
		editor.putBoolean(VISIBLE_IMAGE, true);
		editor.putBoolean(VISIBLE_COORDINAT, false);
		editor.putInt(LINE_WEIGTH, 1);
		editor.putInt(STATISTIC, 10);
		editor.commit();
	}
	
}

